<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="__PUBLIC__/layui/css/layui.css"  media="all">
    <link rel="stylesheet" href="__PUBLIC__/font-awesome/css/font-awesome.min.css" media="all" />
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<div style="margin-top: 20px;">
</div>
<form class="layui-form" id="admin">
    <div class="layui-form-item">
        <label class="layui-form-label">文章：</label>
        {volist name="$id_arr" id="vo" key="key"}
        <div class="layui-input-block" style="width: 90%">
            <div class="layui-input-inline" style="width: 4%;margin-top:4px;">
                {$key}：
            </div>
            <div class="layui-input-inline" style="width: 70%;">
                <select class="article_id" data-id="{$key}" name="article_id_{$key}" lay-verify="required" lay-search>
                    <option value="0">无</option>
                    {volist name="$article" id="vo1"}
                    <option value="{$vo1.id}" {if $vo1.id==$vo}selected{/if}>{$vo1.title}</option>
                    {/volist}
                </select>
            </div>
        </div>
        <br>
        {/volist}
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="admin">立即提交</button>
        </div>
    </div>

</form>


<script src="__PUBLIC__/layui/layui.js"></script>
<script src="__PUBLIC__/jquery/jquery.min.js"></script>
<script>
    var message;
    layui.config({
        base: '__JS__/',
        version: '1.0.1'
    }).use(['app', 'message'], function() {
        var app = layui.app,
            $ = layui.jquery,
            layer = layui.layer;
        //将message设置为全局以便子页面调用
        message = layui.message;
        //主入口
        app.set({
            type: 'iframe'
        }).init();
    });
</script>
<script>
    layui.use(['layer', 'form'], function() {
        var layer = layui.layer,
            $ = layui.jquery,
            form = layui.form;
        $(window).on('load', function() {
            form.on('submit(admin)', function(data) {
                var id_str = '';
                var arr=[];
                var repeadResFlag = 0;
                var repeadResHtml = '';
                $(".article_id").each(function(){
                    var article_id = $(this).val();

                    //检查是否有重复选择
                    var repeadRes = arr.some(function (v) {
                        return article_id === v;
                    });
                    if(repeadRes==true){
                        repeadResFlag = 1;
                        repeadResHtml = $(this).find("option:selected").text();
                    }

                    var id = $(this).attr("data-id");
                    if(article_id!=="0"){
                        id_str += ","+id+"_"+article_id;
                        arr.push(article_id);
                    }
                });

                if(repeadResFlag>0){
                    layer.msg("请勿推荐重复攻略："+repeadResHtml);
                    return false;
                }

                if(id_str !== ''){
                    id_str = id_str.substring(1, id_str.length);
                }

                $.ajax({
                    url:"{:url('admin/index/setArticle')}",
                    data:{idstr:id_str},
                    type:'post',
                    async: false,
                    success:function(res) {
                        layer.msg(res.msg);
                        if(res.code == 1) {
                            setTimeout(function(){
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                            },1500)
                        }
                    }
                })
                return false;
            });
        });
    });
</script>
</body>
</html>
